#include <stdio.h>
#include "NavHelper.h"
#include "ResLoader.h"
#include "PathFinder.h"
#include "../Detour/DetourNavMeshQuery.h"
#include "./TempObstaclesNavMesh.h"

namespace NavHelper
{
	TempObstaclesNavMesh* GetNavMeshTO(std::string name)
	{
		//TONavemeshMap::iterator iter = m_tempobstaclesNavemeshMap.find(name);
		//if (iter != m_tempobstaclesNavemeshMap.end())
		//{
		//	return iter->second;
		//}
		TempObstaclesNavMesh* toNavMesh = ResLoader::LoadTempObstaclesNavMesh(name);
		if (!toNavMesh)
		{
			//printf("NavHelper::GetNavMeshTO fail, ResLoader::GetNavMeshTO name: %s\n", name);
			return NULL;
		}
		//m_tempobstaclesNavemeshMap.insert(std::make_pair(name, toNavMesh));
		//printf("NavHelper::GetNavMeshTO %s load success! \n", name);
		return toNavMesh;
	}

	void ReleaseMapTO()
	{
		//printf("ReleaseMapTO ok! \n");
		//for (TONavemeshMap::iterator iter = m_tempobstaclesNavemeshMap.begin(); iter != m_tempobstaclesNavemeshMap.end(); ++iter)
		//{
		//	delete iter->second;
		//}
		//m_tempobstaclesNavemeshMap.clear();
	}

	float* FindPathTO(TempObstaclesNavMesh* toNavMesh, float& sposX, float& sposY, float& sposZ, float& eposX, float& eposY, float& eposZ, int* count)
	{
		if (!toNavMesh->Find(sposX, sposY, sposZ, eposX, eposY, eposZ))
		{
			*count = 0;
			return nullptr;
		}
		return toNavMesh->GetPath(count);
	}

	bool FindPathExTO(TempObstaclesNavMesh* toNavMesh, float& sposX, float& sposY, float& sposZ, float& eposX, float& eposY, float& eposZ)
	{
		return toNavMesh->Find(sposX, sposY, sposZ, eposX, eposY, eposZ);
	}

	float* GetPathTO(TempObstaclesNavMesh* toNavMesh, int* count)
	{
		return toNavMesh->GetPath(count);
	}

	float* CalcNearTO(TempObstaclesNavMesh* toNavMesh, float& sposX, float& sposY, float& sposZ,int* count)
	{
		*count = 0;
		if (!toNavMesh->CalcNear(sposX, sposY, sposZ))
		{
			return nullptr;
		}
		return toNavMesh->GetNear(count);
	}

	float* CalcRaycastTO(TempObstaclesNavMesh* toNavMesh, float& sposX, float& sposY, float& sposZ, float& eposX, float& eposY, float& eposZ, int* count)
	{
		
		if (!toNavMesh->CalcRaycast(sposX, sposY, sposZ, eposX, eposY, eposZ))
		{
			*count = 0;
			return nullptr;
		}
		return toNavMesh->GetRaycast(count);

	}
	
	void AddCylinderTO(TempObstaclesNavMesh* toNavMesh, std::string tag, float& posX, float& posY, float& posZ, float& radius, float& height)
	{
		toNavMesh->AddCylinderTO(tag, posX, posY, posZ, radius, height);
	}
	void AddObbBox(TempObstaclesNavMesh* toNavMesh, std::string tag, float& centX, float& centY, float& centZ, float& halfX, float& halfY, float& halfZ, float& rotaY)
	{
		toNavMesh->AddObbBox(tag, centX, centY, centZ, halfX, halfY, halfZ, rotaY);
	
	}
	void AddAabbBox(TempObstaclesNavMesh* toNavMesh, std::string tag, float& minX, float& minY, float& minZ, float& maxX, float& maxY, float& maxZ)
	{
		toNavMesh->AddAabbBox(tag, minX, minY, minZ, maxX, maxY, maxZ);
	}
	void RemoveTOByTag(TempObstaclesNavMesh* toNavMesh, std::string tag)
	{
		toNavMesh->RemoveTOByTag(tag);
	}

	void RemoveAllTO(TempObstaclesNavMesh* toNavMesh)
	{
		toNavMesh->RemoveAllTO();
	}

	void Update(TempObstaclesNavMesh* toNavMesh)
	{
		toNavMesh->Update();
	}

	void FreeToNavMesh(TempObstaclesNavMesh* toNavMesh)
	{
		delete toNavMesh;
	}
}
